package com.yitongmao.activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;

import com.google.gson.Gson;
import com.yitongmao.bean.LoginHuancunBean;
import com.yitongmao.core.cache.ACache;
import com.yitongmao.util.GlideCacheUtil;
import com.yitongmao.yitmfruit.R;
import com.zhy.autolayout.AutoLinearLayout;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class UserSetActivity extends AppCompatActivity {
    @Bind(R.id.mGeRenXinxi)
    AutoLinearLayout mGeRenXinxi;
    @Bind(R.id.mXiuGaiMiMa)
    AutoLinearLayout mXiuGaiMiMa;
    @Bind(R.id.mBangDingShouji)
    AutoLinearLayout mBangDingShouji;
    @Bind(R.id.mQingChuHuanCun)
    AutoLinearLayout mQingChuHuanCun;
    @Bind(R.id.gender)
    TextView gender;
    @Bind(R.id.user_huancun)
    TextView userHuancun;
    @Bind(R.id.mOutLogin)
    Button mOutLogin;

    private LoginHuancunBean loginHuancunBean;
    private int userId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        immerSion();
        setContentView(R.layout.activity_user_set);
        ButterKnife.bind(this);
        //获取缓存大小
        userHuancun.setText(GlideCacheUtil.getInstance().getCacheSize(UserSetActivity.this));
        Gson gson = new Gson();
        String loginId = ACache.get(UserSetActivity.this).getAsString("loginId");
        if (loginId != null) {
            loginHuancunBean = gson.fromJson(loginId, LoginHuancunBean.class);
            userId = loginHuancunBean.userInfo.id;

            String phone = loginHuancunBean.userInfo.phone;
            gender.setText(phone);
        }
    }
    @OnClick({R.id.mGeRenXinxi, R.id.mXiuGaiMiMa, R.id.mBangDingShouji, R.id.mQingChuHuanCun, R.id.mOutLogin, R.id.zhucehuitui})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.mGeRenXinxi:
                Intent intent = new Intent(UserSetActivity.this, MyPersonaldatailsActivity.class);
                startActivity(intent);
                break;
            case R.id.mXiuGaiMiMa:
                Intent intent1 = new Intent(UserSetActivity.this, ForgetPassWordActivity.class);
                startActivity(intent1);
                break;
            case R.id.mQingChuHuanCun:
                dialog();
                break;
            case R.id.mOutLogin:
                ACache.get(UserSetActivity.this).clear();
                Intent intent2 = new Intent(UserSetActivity.this, MainActivity.class);
                startActivity(intent2);
                finish();
                break;
            case R.id.zhucehuitui:
                UserSetActivity.this.finish();
                break;
        }
    }
    //清除缓存dialog
    private void dialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);  //先得到构造器
        // builder.setTitle("提示"); //设置标题
        builder.setMessage("是否清除缓存？"); //设置内容
        // builder.setIcon(R.mipmap.ic_launcher);//设置图标，图片id即可
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() { //设置确定按钮
            @Override
            public void onClick(DialogInterface dialog, int which) {
                GlideCacheUtil.getInstance().clearImageAllCache(UserSetActivity.this);
                //获取缓存大小
                userHuancun.setText(GlideCacheUtil.getInstance().getCacheSize(UserSetActivity.this));
                dialog.dismiss(); //关闭dialog
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() { //设置取消按钮
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        //参数都设置完成了，创建并显示出来
        builder.create().show();
    }

    public void immerSion() {
        getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
                    | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);
            window.setNavigationBarColor(Color.BLACK);
        }
    }


}
